//
//  MeViewController.m
//  WeChat
//
//  Created by wangcarl on 16/8/30.
//  Copyright © 2016年 wangcarl. All rights reserved.
//

#import "MeViewController.h"
#import "XMPPvCardTemp.h"
#import "EditVCardViewController.h"

@interface MeViewController ()<EditVCDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *avatarImgView;//头像
@property (weak, nonatomic) IBOutlet UILabel *nicknameLabel;//昵称
@property (weak, nonatomic) IBOutlet UILabel *wechatNumLabel;//微信号
@property (weak, nonatomic) IBOutlet UILabel *orgNameLabel;//公司
@property (weak, nonatomic) IBOutlet UILabel *departmentLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;//职位

@property (weak, nonatomic) IBOutlet UILabel *telLabel;//电话

@property (weak, nonatomic) IBOutlet UILabel *emailLabel;

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     XMPPvCardTemp *myvCard= [XMPPTool sharedXMPPTool].vCard.myvCardTemp;
    if (myvCard.photo) {
        self.avatarImgView.image  =[UIImage imageWithData:myvCard.photo];
    }
    
    self.wechatNumLabel.text=[Account shareAccount].user;
    self.nicknameLabel.text=myvCard.nickname;
    self.orgNameLabel.text=myvCard.orgName;
    if (myvCard.orgUnits.count>0) {
        self.departmentLabel.text=myvCard.orgUnits[0];
        
    }
    self.titleLabel.text    =myvCard.title;
    self.telLabel.text=myvCard.note;
    self.emailLabel.text=myvCard.mailer;
//    if (myvCard.telecomsAddresses.count>0) {
//        
//        self.telLabel.text=myvCard.telecomsAddresses[0];
//        self.emailLabel.text=myvCard.mailer;
//    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *selectedCell=  [tableView cellForRowAtIndexPath:indexPath];;
    switch (selectedCell.tag) {
        case 0:
            [self choseImg];
            break;
        case 1:
            [self performSegueWithIdentifier:@"toWditVcSegue" sender:selectedCell];
            
            
            break;
        case 2:
            
            break;
        case 3:
            
            break;
        case 4:
            [self performSegueWithIdentifier:@"toWditVcSegue" sender:selectedCell];

            break;
        case 5:
            [self performSegueWithIdentifier:@"toWditVcSegue" sender:selectedCell];

            break;
        case 6:
            [self performSegueWithIdentifier:@"toWditVcSegue" sender:selectedCell];

            break;
        case 7:
            [self performSegueWithIdentifier:@"toWditVcSegue" sender:selectedCell];

            break;

        default:
            break;
    }

}
-(void)choseImg
{
    UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:@"请选测" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"照相" otherButtonTitles:@"图片库", nil];
    [sheet showInView:self.view];
    
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==2) return;
    
    UIImagePickerController *imgPC=[[UIImagePickerController alloc] init];
    imgPC.delegate=self;
    imgPC.allowsEditing=YES;
    if (buttonIndex==0) {
        imgPC.sourceType=UIImagePickerControllerSourceTypeCamera;
    }else
    {
        imgPC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    [self presentViewController:imgPC animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
   UIImage *editingImg= info[UIImagePickerControllerEditedImage];
    self.avatarImgView.image=editingImg;
    [self dismissViewControllerAnimated:YES completion:nil];
    [self editVCardViewController:nil didFinishedSave:nil];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id destVC=segue.destinationViewController;
    if ([destVC isKindOfClass:[EditVCardViewController class]]) {
        EditVCardViewController *editVc=destVC;
        editVc.cell=sender;
        editVc.delegate=self;
    }
}

-(void)editVCardViewController:(EditVCardViewController *)editVc didFinishedSave:(id)sender
{
    XMPPvCardTemp *myVCard=[XMPPTool sharedXMPPTool].vCard.myvCardTemp;
   
    myVCard.photo=UIImageJPEGRepresentation(self.avatarImgView.image, 0.75);
    
    myVCard.nickname=self.nicknameLabel.text;
    myVCard.orgName=self.orgNameLabel.text;
    if (self.departmentLabel.text!=nil) {
        myVCard.orgUnits=@[self.departmentLabel.text];
    }
    myVCard.title=self.titleLabel.text;
    myVCard.note=self.telLabel.text;
    myVCard.mailer=self.emailLabel.text;
    [[XMPPTool sharedXMPPTool].vCard updateMyvCardTemp:myVCard];

}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 0;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
//    return 0;
//}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
